package com.own.demo.建筑物;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.own.demo.utils.GeoTools;
import com.own.demo.utils.geo.PoiTypeEnums;
import com.own.demo.utils.geo.buffer.BufferBase;
import com.own.demo.utils.geo.buffer.LineBufferImpl;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.Point;

import java.util.ArrayList;
import java.util.List;


public class 扣除建筑物逻辑处理 {
    public static LineBufferImpl defaultBuffer = (LineBufferImpl) PoiTypeEnums.POLYLINE.getBufferUtil();

    public static void buildingDel(JSONArray jsonArray) {
        int row = jsonArray.size();
        System.out.println(row);

        List<LineString> allList=new ArrayList<>(1024);
        allList.addAll(扣除建筑物.floorHeightMap.get(1));
        allList.addAll(扣除建筑物.floorHeightMap.get(2));
        for (int i = 0; i < row; i++) {
            LineString lineString = defaultBuffer.getLineString(jsonArray.getJSONObject(i));
            System.out.println(lineString);

        }
    }

    /**
     * 去除空洞部分
     * @return 空洞后的线
     */
    public static JSONArray dataInfo(LineString line, String geoType, List<LineString> allList,int index) {
        JSONArray jsonArray = new JSONArray(1024);

        int indexSize=allList.size();
        List<Geometry> intersection=new ArrayList<>(120);
        for(int i=0;i<indexSize;i++){
            LineString build=allList.get(i);
            Geometry geometryIn = line.intersection(build);
            //相交
                if (!geometryIn.isEmpty()) {
                    intersection.add(geometryIn);
//                    IntersercionInfo intersercionInfo=new IntersercionInfo()
    //                JSONArray gpsList = new JSONArray(coordRow);
    //                Coordinate[] coordinates1 = geometryIn.getBoundary().getCoordinates();
    //                for (int h = 0; h < coordRow; h++) {
    //                    JSONArray array = points.getJSONArray(h);
    //                    boolean flag = GeoTools.onLineSegment(new Coordinate(array.getDouble(0),array.getDouble(1)), coordinates1[0], coordinates1[1]);
    //                    if (flag) {
    //                        JSONArray gps = gpsList.getJSONArray(gpsList.size() - 1);
    //                        double distance1 = GeoTools.distance(new Coordinate(gps.getDouble(0), gps.getDouble(1)), coordinates1[0]);
    //                        double distance2 = GeoTools.distance(new Coordinate(gps.getDouble(0), gps.getDouble(1)), coordinates1[1]);
    //
    //                        double[] gps1 = new double[2];
    //                        gps1[0] = coordinates1[0].x;
    //                        gps1[1] = coordinates1[0].y;
    //
    //                        double[] gps2 = new double[2];
    //                        gps2[0] = coordinates1[1].x;
    //                        gps2[1] = coordinates1[1].y;
    //
    //                        gpsList.add(distance1 <= distance2 ? gps1 : gps2);
    //                        addLingString(jsonArray, i, 0, geoType, gpsList);
    //                        addLingString(jsonArray, i, 1 + 1, geoType, addOther(h, points, coordRow, !(distance1 <= distance2) ? gps1 : gps2, coordinates1));
    //                        break;
    //                    }
    //                    gpsList.add(array);
                    }
    //
    //            //包含
    //            }else if(line.within(build)){
    //
    //            }else {
    //                addLingString(jsonArray, i, 0, geoType, line.getCoordinates());
    //            }
        }
         int row=intersection.size();
        if(row>0){

            List<List<Coordinate>> dataList=new ArrayList<>();
            Coordinate[] lineArr= line.getCoordinates();
            int lineSize=lineArr.length;
            for(int j=0;j<lineSize;j++){

                List<Coordinate> listINfos=new ArrayList<>();

                Coordinate temp= lineArr[j];
                Point point=BufferBase.geometryFactory.createPoint(temp);

                boolean flag=false;
                for(int i=0;i<row;i++){
                    Geometry  geometry=intersection.get(i);
                    Geometry gInfo=  geometry.intersection(point);
                    if(gInfo.isEmpty()){
                        flag=true;
                        dataList.add(new ArrayList<>(listINfos));
                        //上一段已经结束
                        listINfos=new ArrayList<>();
                        listINfos.add(temp);
                        break;
                    }
                }

                listINfos.add(temp);

            }
        }else{
            addLingString(jsonArray, index, 0, geoType, line.getCoordinates());
        }
        return jsonArray;
    }


    public static void addLingString(JSONArray jsonArray, int i, int j, String geoType, Coordinate[] gpsList) {
        JSONObject geo = new JSONObject(4);
        geo.put("type", "Feature");
        JSONObject geoPro = new JSONObject(1);
        geoPro.put("name", i > 0 ? j + "-" + i : "area");
        geo.put("properties", geoPro);
        JSONObject objectInfo = new JSONObject();
        int row=gpsList.length;
        JSONArray objects=new JSONArray(row);
        for(int h=0;h<row;h++){
            Coordinate coordinate=gpsList[h];
            JSONArray temp=new JSONArray(2);
            temp.add(coordinate.x);
            temp.add(coordinate.y);
        }
        objectInfo.put("coordinates", objects);
        objectInfo.put("type", geoType);
        geo.put("geometry", objectInfo);
        jsonArray.add(geo);
    }
}
